<template>
  <div>
    <el-form ref="perfectFormRef" :model="userInfo" :rules="rules" label-width="100px" class="perfect-info">
      <el-form-item label="所在城市" prop="county">
        <el-cascader filterable size="mini" :options="options" placeholder="所在城市" v-model="selectedOptions"
                     @change="handleCityChange"/>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="userInfo.realName" placeholder="请输入真实姓名" clearable/>
      </el-form-item>
      <el-tooltip class="item" effect="dark" content="身份证号码是唯一的哦~" placement="top-start">
        <el-form-item label="身份证号码" prop="idNumber">
          <el-input v-model="userInfo.idNumber" placeholder="请输入身份证号码" clearable maxlength="18"
                    minlength="15"/>
        </el-form-item>
      </el-tooltip>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="userInfo.address" placeholder="请输入详细地址" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="submit">保存</el-button>
        <el-button type="danger" size="mini" @click="close">关闭</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { reactive, toRefs, ref, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'
import memberApi from '../../../api/member'
import { CodeToText, regionDataPlus, TextToCode } from 'element-china-area-data'

export default {
  props: {
    userInfo: {
      type: Object
    }
  },
  name: 'perfectInfo',
  setup (props) {
    let store = useStore()
    let router = useRouter()
    let perfectFormRef = ref(null)
    let state = reactive({
      form: {},
      // 市区级联
      options: regionDataPlus,
      selectedOptions: [],
      rules: {
        county: [
          { required: true, message: '所在城市不能为空', trigger: 'change' }
        ],
        realName: [
          { required: true, message: '真实姓名不能为空', trigger: 'change' }
        ],
        address: [
          { required: true, message: '详细地址不能为空', trigger: 'change' }
        ],
        idNumber: [
          { required: true, message: '身份证号码不能为空', trigger: 'change' },
          {
            pattern: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/,
            message: '请输入正确的身份证号码',
            trigger: 'change'
          }
        ],
      }
    })
    // 级联选择的值
    const handleCityChange = (arr) => {
      let province = CodeToText[arr[0]]
      let city = CodeToText[arr[1]]
      let county = CodeToText[arr[2]]
      props.userInfo.province = province
      props.userInfo.city = city
      props.userInfo.county = county
    }
    // 初始化城市选择
    const initCitySelection = () => {
      let province = props.userInfo.province
      let city = props.userInfo.city
      let county = props.userInfo.county
      if (province && city && county) {
        state.selectedOptions = [TextToCode[province].code, TextToCode[province][city].code, TextToCode[province][city][county].code]
      }
    }
    // 提交
    const submit = () => {
      perfectFormRef.value.validate(valid => {
        if (valid) {
          memberApi.perfectMemberInfo({
            province: props.userInfo.province,
            city: props.userInfo.city,
            county: props.userInfo.county,
            realName: props.userInfo.realName,
            idNumber: props.userInfo.idNumber,
            address: props.userInfo.address,
          }).then(res => {
            store.dispatch('GetInfo').then(() => {
              ElMessage.success('完善成功')
            })
          })
        }
      })
    }
    // 关闭
    // 关闭
    const close = () => {
      router.push('/home')
    }
    //---------------- 钩子
    onMounted(() => {
      initCitySelection()
    })
    return {
      ...toRefs(state),
      perfectFormRef,
      handleCityChange,
      submit,
      close

    }
  }
}
</script>

<style scoped lang="scss">
.perfect-info::v-deep .el-cascader.el-cascader--mini {
  width: 450px;
}
</style>
